import 'package:flutter/material.dart';
import 'package:flutterapp/common/widget/easy_ink_well.dart';

/// 显示带有两个底部按钮得提示弹窗
///
/// [context] 上下文
///
/// [title]  标题
///
/// [content] 内容
///
/// [leftOrTopBtnText] 左侧或者上侧按钮内容
///
/// [rightOrBottomBtnText] 右侧或下侧按钮内容
///
/// [leftOrTopOnTap] 左侧或者上侧按钮点击事件
///
/// [rightOrBottomOnTap] 右侧或者下侧按钮点击事件
///
/// [barrierDismissible] 点击弹窗外面是否可以关闭弹窗
///
/// [contentTextAlign] 内容对齐方式
///
/// [type] 弹窗类型
///
/// [child] 子类组件，该属性只有[type]属性值为[EasyDialogType.Custom]的时候才生效，用于自定义弹窗
///
Future<T?> showEasyDialog<T>({
  required BuildContext context,
  String title = '温馨提示',
  String content = '',
  String leftOrTopBtnText = '取消',
  String rightOrBottomBtnText = '确定',
  ValueChanged<BuildContext>? leftOrTopOnTap,
  ValueChanged<BuildContext>? rightOrBottomOnTap,
  bool barrierDismissible = true,
  TextAlign contentTextAlign = TextAlign.center,
  EasyDialogType type = EasyDialogType.GotIt,
  Widget? child,
}) {
  return showDialog<T>(
    context: context,
    barrierDismissible: barrierDismissible,
    builder: (bContext) {
      return Dialog(
        backgroundColor: Colors.black.withOpacity(.25),
        child: _getDialogChild(
          title: title,
          content: content,
          leftOrTopBtnText: leftOrTopBtnText,
          rightOrBottomBtnText: rightOrBottomBtnText,
          leftOrTopOnTap: leftOrTopOnTap,
          rightOrBottomOnTap: rightOrBottomOnTap,
          contentTextAlign: contentTextAlign,
          type: type,
          child: child,
        ),
      );
    },
  );
}

/// 获取弹窗内容
Widget? _getDialogChild({
  String title = '提示',
  String content = '',
  String leftOrTopBtnText = '取消',
  String rightOrBottomBtnText = '确定',
  ValueChanged<BuildContext>? leftOrTopOnTap,
  ValueChanged<BuildContext>? rightOrBottomOnTap,
  TextAlign contentTextAlign = TextAlign.center,
  EasyDialogType type = EasyDialogType.GotIt,
  Widget? child,
}) {
  switch (type) {
    case EasyDialogType.GotIt:
      return _GotItTipsWidget(
        title: title,
        content: content,
        btnText: leftOrTopBtnText,
        onTap: leftOrTopOnTap,
        contentTextAlign: contentTextAlign,
      );

    case EasyDialogType.NormalTwoRowBtn:
    case EasyDialogType.NotContentTwoRowBtn:
      return _TwoRowBtnTipsWidget(
        title: title,
        content: content,
        lBtnText: leftOrTopBtnText,
        rBtnText: rightOrBottomBtnText,
        lOnTap: leftOrTopOnTap,
        rOnTap: rightOrBottomOnTap,
        contentTextAlign: contentTextAlign,
        type: type,
      );
    case EasyDialogType.NormalTwoColumnBtn:
      return _TwoColumnBtnTipsWidget(
        title: title,
        content: content,
        tBtnText: leftOrTopBtnText,
        bBtnText: rightOrBottomBtnText,
        tOnTap: leftOrTopOnTap,
        bOnTap: rightOrBottomOnTap,
        contentTextAlign: contentTextAlign,
      );
    case EasyDialogType.Custom:
      return child;
    default:
      return Container();
  }
}

/// 弹窗类型
enum EasyDialogType {
  /// 含有标题、内容、底部一个按钮(常用于我知道了等内容提示弹窗)
  GotIt,

  /// 含有标题、内容、底部水平排列的两个按钮(常用于操作提示弹窗)
  NormalTwoRowBtn,

  /// 含有标题、底部水平排列的两个按钮(常用于强调提示弹窗)
  NotContentTwoRowBtn,

  /// 含有标题、底部竖直排列的两个按钮(常用于强调提示弹窗)
  NormalTwoColumnBtn,

  /// 自定义弹窗类型
  Custom,
}

/// 含有两个水平按钮的提示组件
class _TwoRowBtnTipsWidget extends StatelessWidget {
  /// 标题
  final String title;

  /// 内容
  final String content;

  /// 左侧按钮内容
  final String lBtnText;

  ///  右侧按钮内容
  final String rBtnText;

  /// 左侧按钮点击事件
  final ValueChanged<BuildContext>? lOnTap;

  ///  右侧按钮点击事件
  final ValueChanged<BuildContext>? rOnTap;

  ///  内容文本对齐方式
  final TextAlign contentTextAlign;

  /// 类型
  final EasyDialogType type;

  const _TwoRowBtnTipsWidget({
    Key? key,
    this.title = '温馨提示',
    this.content = '',
    this.lBtnText = '取消',
    this.rBtnText = '确定',
    this.lOnTap,
    this.rOnTap,
    this.contentTextAlign = TextAlign.center,
    required this.type,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(8.0),
      ),
      padding: const EdgeInsets.fromLTRB(24, 32, 24, 24),
      child: SingleChildScrollView(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            //标题
            Text(
              title,
              style: TextStyle(
                fontSize: 20,
                color: Colors.black.withOpacity(.85),
                fontWeight: FontWeight.bold,
              ),
            ),
            //内容
            Visibility(
              visible: type != EasyDialogType.NotContentTwoRowBtn,
              child: Container(
                margin: const EdgeInsets.only(
                  top: 16,
                ),
                child: Text(
                  content,
                  textAlign: contentTextAlign,
                  style: TextStyle(
                    color: Colors.black45,
                    fontSize: 16,
                  ),
                ),
              ),
            ),
            SizedBox(
              height: 32,
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                //底部左侧按钮
                Expanded(
                  child: EasyInkWell(
                    onTap: () {
                      lOnTap?.call(context);
                    },
                    radius: 4,
                    color: Colors.white,
                    child: Container(
                      alignment: Alignment.center,
                      decoration: BoxDecoration(
                        border: Border.all(
                          color: Color(0xFF2F73F9).withOpacity(.25),
                          width: 1,
                        ),
                        borderRadius: BorderRadius.circular(4),
                      ),
                      padding: const EdgeInsets.symmetric(vertical: 8),
                      child: Text(
                        lBtnText,
                        style: TextStyle(
                            color: Color(0xFF2F73F9),
                            fontSize: 16,
                            fontWeight: FontWeight.bold),
                      ),
                    ),
                  ),
                ),
                SizedBox(width: 12),
                //底部右侧按钮
                Expanded(
                  child: EasyInkWell(
                    onTap: () {
                      rOnTap?.call(context);
                    },
                    radius: 4,
                    gradient: LinearGradient(
                        colors: [Color(0xFF4182FF), Color(0xFF2E5BFF)]),
                    child: Container(
                      alignment: Alignment.center,
                      padding: const EdgeInsets.symmetric(vertical: 8),
                      child: Text(
                        rBtnText,
                        style: TextStyle(
                            color: Colors.white,
                            fontSize: 16,
                            fontWeight: FontWeight.bold),
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}

class _TwoColumnBtnTipsWidget extends StatelessWidget {
  /// 标题
  final String title;

  /// 内容
  final String content;

  /// 上侧按钮内容
  final String tBtnText;

  ///  下侧按钮内容
  final String bBtnText;

  /// 上侧按钮点击事件
  final ValueChanged<BuildContext>? tOnTap;

  ///  下侧按钮点击事件
  final ValueChanged<BuildContext>? bOnTap;

  ///  内容文本对齐方式
  final TextAlign contentTextAlign;

  const _TwoColumnBtnTipsWidget({
    Key? key,
    this.title = '温馨提示',
    this.content = '',
    this.contentTextAlign = TextAlign.center,
    this.tBtnText = '取消',
    this.bBtnText = '确定',
    this.tOnTap,
    this.bOnTap,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(8.0),
      ),
      padding: const EdgeInsets.fromLTRB(24, 32, 24, 24),
      child: SingleChildScrollView(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            //标题
            Text(
              title,
              style: TextStyle(
                fontSize: 20,
                color: Colors.black.withOpacity(.85),
                fontWeight: FontWeight.bold,
              ),
            ),
            //内容
            Container(
              margin: const EdgeInsets.only(
                top: 16,
              ),
              child: Text(
                content,
                textAlign: contentTextAlign,
                style: TextStyle(
                  color: Colors.black45,
                  fontSize: 16,
                ),
              ),
            ),
            SizedBox(
              height: 32,
            ),
            EasyInkWell(
              onTap: () {
                tOnTap?.call(context);
              },
              radius: 4,
              gradient: LinearGradient(
                  colors: [Color(0xFF4182FF), Color(0xFF2E5BFF)]),
              child: Container(
                alignment: Alignment.center,
                padding: const EdgeInsets.symmetric(vertical: 8),
                child: Text(
                  tBtnText,
                  style: TextStyle(
                      color: Colors.white,
                      fontSize: 16,
                      fontWeight: FontWeight.bold),
                ),
              ),
            ),
            SizedBox(height: 12),
            //底部下侧按钮
            EasyInkWell(
              onTap: () {
                bOnTap?.call(context);
              },
              radius: 4,
              color: Colors.transparent,
              child: Container(
                alignment: Alignment.center,
                padding: const EdgeInsets.symmetric(vertical: 8),
                child: Text(
                  bBtnText,
                  style: TextStyle(
                      color: Color(0xFF2F73F9),
                      fontSize: 16,
                      fontWeight: FontWeight.bold),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

/// 知道了提示组件
class _GotItTipsWidget extends StatelessWidget {
  /// 标题
  final String title;

  /// 内容
  final String content;

  /// 按钮内容
  final String btnText;

  ///  按钮点击事件
  final ValueChanged<BuildContext>? onTap;

  ///  内容文本对齐方式
  final TextAlign contentTextAlign;

  const _GotItTipsWidget({
    Key? key,
    this.title = '',
    this.content = '',
    this.btnText = '',
    this.onTap,
    this.contentTextAlign = TextAlign.center,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(8.0),
      ),
      padding: const EdgeInsets.fromLTRB(24, 32, 24, 24),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          //标题
          Text(
            title,
            style: TextStyle(
              fontSize: 20,
              color: Colors.black.withOpacity(.85),
              fontWeight: FontWeight.bold,
            ),
          ),
          //内容
          Container(
            margin: const EdgeInsets.only(
              top: 16,
            ),
            child: Text(
              content,
              textAlign: contentTextAlign,
              style: TextStyle(
                color: Colors.black45,
                fontSize: 16,
              ),
            ),
          ),
          SizedBox(
            height: 32,
          ),
          //底部按钮
          TextButton(
            onPressed: () {
              onTap?.call(context);
            },
            child: Text(
              btnText,
              style: TextStyle(
                  color: Color(0xFF2F73F9),
                  fontSize: 16,
                  fontWeight: FontWeight.bold),
            ),
          ),
        ],
      ),
    );
  }
}
